home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / libpcap-0.0.6 / scanner.l < prev    next >
Encoding:
Lex Description  |  1994-06-11  |  4.1 KB  |  194 lines

  1. %{
  2. /*
  3.  * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994
  4.  *    The Regents of the University of California.  All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that: (1) source code distributions
  8.  * retain the above copyright notice and this paragraph in its entirety, (2)
  9.  * distributions including binary code include the above copyright notice and
  10.  * this paragraph in its entirety in the documentation or other materials
  11.  * provided with the distribution, and (3) all advertising materials mentioning
  12.  * features or use of this software display the following acknowledgement:
  13.  * ``This product includes software developed by the University of California,
  14.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  15.  * the University nor the names of its contributors may be used to endorse
  16.  * or promote products derived from this software without specific prior
  17.  * written permission.
  18.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  19.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  20.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  21.  */
  22.  
  23. #ifndef lint
  24. static char rcsid[] =
  25.     "@(#) $Header: scanner.l,v 1.40 94/06/10 17:21:44 mccanne Exp $ (LBL)";
  26. #endif
  27.  
  28. #include <sys/types.h>
  29. #include <sys/time.h>
  30.  
  31. #include <ctype.h>
  32. #include <pcap.h>
  33. #include <pcap-namedb.h>
  34.  
  35. #include "gencode.h"
  36. #include "tokdefs.h"
  37.  
  38. #ifndef __GNUC__
  39. #define inline
  40. #endif
  41.  
  42. static int stoi(char *);
  43. static inline int xdtoi(int);
  44.  
  45. #ifdef FLEX_SCANNER
  46. #undef YY_INPUT
  47. #define YY_INPUT(buf, result, max)\
  48.  {\
  49.     char *src = in_buffer;\
  50.     int i;\
  51. \
  52.     if (*src == 0)\
  53.         result = YY_NULL;\
  54.     else {\
  55.         for (i = 0; *src && i < max; ++i)\
  56.             buf[i] = *src++;\
  57.         in_buffer += i;\
  58.         result = i;\
  59.     }\
  60.  }
  61. #else
  62. #undef getc
  63. #define getc(fp)  (*in_buffer == 0 ? EOF : *in_buffer++)
  64. #endif
  65.  
  66. #define yylval pcap_lval
  67. extern YYSTYPE yylval;
  68.  
  69. static char *in_buffer;
  70.  
  71. %}
  72.  
  73. N        ([0-9]+|(0X|0x)[0-9A-Fa-f]+)
  74. B        ([0-9A-Fa-f][0-9A-Fa-f]?)
  75.  
  76. %a 3000
  77.  
  78. %%
  79. dst        return DST;
  80. src        return SRC;
  81.  
  82. link|ether|ppp|slip  return LINK;
  83. fddi        return LINK;
  84. arp        return ARP;
  85. rarp        return RARP;
  86. ip        return IP;
  87. tcp        return TCP;
  88. udp        return UDP;
  89. icmp        return ICMP;
  90.  
  91. decnet        return DECNET;
  92. lat        return LAT;
  93. moprc        return MOPRC;
  94. mopdl        return MOPDL;
  95.  
  96. host        return HOST;
  97. net        return NET;
  98. port        return PORT;
  99. proto        return PROTO;
  100.  
  101. gateway        return GATEWAY;
  102.  
  103. less        return LESS;
  104. greater        return GREATER;
  105. byte        return BYTE;
  106. broadcast    return TK_BROADCAST;
  107. multicast    return TK_MULTICAST;
  108.  
  109. and|"&&"    return AND;
  110. or|"||"        return OR;
  111. not        return '!';
  112.  
  113. len|length    return LEN;
  114. inbound        return INBOUND;
  115. outbound    return OUTBOUND;
  116.  
  117. [ \n\t]            ;
  118. [+\-*/:\[\]!<>()&|=]    return yytext[0];
  119. ">="            return GEQ;
  120. "<="            return LEQ;
  121. "!="            return NEQ;
  122. "=="            return '=';
  123. "<<"            return LSH;
  124. ">>"            return RSH;
  125. {N}            { yylval.i = stoi((char *)yytext); return NUM; }
  126. ({N}\.{N})|({N}\.{N}\.{N})|({N}\.{N}\.{N}\.{N})    {
  127.             yylval.s = sdup((char *)yytext); return HID;
  128. }
  129. {B}:{B}:{B}:{B}:{B}:{B} { yylval.e = pcap_ether_aton((char *)yytext);
  130.               return EID; }
  131. {B}:+({B}:+)+        { bpf_error("bogus ethernet address %s", yytext); }
  132. [A-Za-z][-_.A-Za-z0-9]*    { yylval.s = sdup((char *)yytext); return ID; }
  133. "\\"[^ !()\n\t]+    { yylval.s = sdup((char *)yytext + 1); return ID; }
  134. [^ \[\]\t\n\-_.A-Za-z0-9!<>()&|=]+    { bpf_error("illegal token: %s\n", yytext); }
  135. .            { bpf_error("illegal char '%c'", *yytext); }
  136. %%
  137. void
  138. lex_init(buf)
  139.     char *buf;
  140. {
  141.     in_buffer = buf;
  142. }
  143.  
  144. /*
  145.  * Also define a yywrap.  Note that if we're using flex, it will
  146.  * define a macro to map this identifier to pcap_wrap.
  147.  */
  148. int
  149. yywrap()
  150. {
  151.     return 1;
  152. }
  153.  
  154. /* Hex digit to integer. */
  155. static inline int
  156. xdtoi(c)
  157.     register int c;
  158. {
  159.     if (isdigit(c))
  160.         return c - '0';
  161.     else if (islower(c))
  162.         return c - 'a' + 10;
  163.     else
  164.         return c - 'A' + 10;
  165. }
  166.  
  167. /*
  168.  * Convert string to integer.  Just like atoi(), but checks for
  169.  * preceding 0x or 0 and uses hex or octal instead of decimal.
  170.  */
  171. static int
  172. stoi(s)
  173.     char *s;
  174. {
  175.     int base = 10;
  176.     int n = 0;
  177.  
  178.     if (*s == '0') {
  179.         if (s[1] == 'x' || s[1] == 'X') {
  180.             s += 2;
  181.             base = 16;
  182.         }
  183.         else {
  184.             base = 8;
  185.             s += 1;
  186.         }
  187.     }
  188.     while (*s)
  189.         n = n * base + xdtoi(*s++);
  190.  
  191.     return n;
  192. }
  193.  
  194.